package com.dmxh.service.impl;

import com.dmxh.mapper.TbContentCategoryMapper;
import com.dmxh.pojo.TbContent;
import com.dmxh.pojo.TbContentCategory;
import com.dmxh.service.ContentCategoryService;
import com.dmxh.vo.ContentCategoryVO;
import com.dmxh.vo.ContentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ContentCategoryImpl implements ContentCategoryService {

    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;

    @Override
    public Map<String, Object> queryContetCategory(Integer pageNum, Integer pageSize, ContentCategoryVO categoryVO) {
//        Page page = new Page();
//        page.setCurrent(pageNum);
//        page.setSize(pageSize);
//
//        /*模糊*/
//        LambdaQueryWrapper<TbContentCategory> wrapper = new LambdaQueryWrapper<>();
//        wrapper.like(categoryVO.getName()!=null,TbContentCategory::getName,categoryVO.getName());
        List<TbContentCategory> tbContentCategories = tbContentCategoryMapper.selectByExample(null);
        List<ContentCategoryVO> collect = tbContentCategories.stream().map(contentcategorylist -> {
            ContentCategoryVO categoryVO1 = new ContentCategoryVO();
            BeanUtils.copyProperties(contentcategorylist, categoryVO1);
            return categoryVO1;
        }).collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("categorylist", collect);
        return map;
    }

    @Override
    public boolean saveContentCategory(ContentCategoryVO categoryVO) {
        TbContentCategory category = new TbContentCategory();
        BeanUtils.copyProperties(categoryVO, category);
        return (tbContentCategoryMapper.insertSelective(category) == 1) ? true : false;
    }

    @Override
    public boolean deleteContentCategory(Long id) {
        return (tbContentCategoryMapper.deleteByPrimaryKey(id) == 1) ? true : false;
    }

    @Override
    public boolean updateContentCategory(ContentCategoryVO categoryVO) {
        TbContentCategory category = new TbContentCategory();
        BeanUtils.copyProperties(categoryVO, category);
        return (tbContentCategoryMapper.updateByPrimaryKeySelective(category) == 1) ? true : false;
    }
}
